package com.winit.openapi.mms.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.mms.spi.category.CategoryService;
import com.winit.mms.spi.vo.CategoryVo;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.mms.vo.WinitCategoryVo;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * 
 * mms万邑通分类操作控制器
 * 
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * hao.gong 	1.0  		2015年5月18日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/mms/winitCategory")
public class WinitCategoryController extends BaseController {

    /*
     * 万邑通分类服务
     */
	@Resource
	private CategoryService categoryService;

	/**
	 * <b>查询万邑通分类</b></p>
	 * TODO Add comments here.
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
	@RequestMapping(value="/winitCategoryList")
	public String findWinitCategory(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
		/*
		 * <pre>1、获取请求参数</pre></br>
		 */
		Object obj = requestMsg.getData();
		String code = "";
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;

			if(json.containsKey("code")){
				code = json.getString("code").trim();
			}

		}

		/*
		 * <pre>2、调用查询mms服务</pre></br>
		 */
		List<CategoryVo> lstCategory = categoryService.queryByCode(code);

		/*
		 * <pre>3、处理构建查询结果</pre></br>
		 */
		List<WinitCategoryVo> lstWinitCategory = new ArrayList<WinitCategoryVo>();
		if(null != lstCategory && lstCategory.size() > 0){
			for (CategoryVo categoryVo : lstCategory) {
				WinitCategoryVo objVo = new WinitCategoryVo();
				objVo.setCode(categoryVo.getCategoryCode());
				objVo.setName(categoryVo.getCategoryName());
				objVo.setParentCode(categoryVo.getCategoryParentCode());
				lstWinitCategory.add(objVo);
			}
		}

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("category", lstWinitCategory);

		responseMsg.setData(data);

		return SUCCESS;
	}

}
